[SECTION .text]
[BITS 64]

extern clock_interrupt_handler
extern get_task_context

extern current

global clock_handler_entry
clock_handler_entry:
    push rdi
    mov rdi, [current]
    cmp rdi, 0
    je .pop_rdi

.store_context:
    push rax
    call get_task_context

    mov [rax + 8 * 3], rbx
    mov [rax + 8 * 4], rcx
    mov [rax + 8 * 5], rdx
    ; mov [rax + 8 * 6], rdi ; 这个还保存不了，在栈中
    mov [rax + 8 * 7], rsi
    mov [rax + 8 * 8], rbp
    ; mov [rax + 8 * 9], rsp ; 这个也保存不了，后面保存
    mov [rax + 8 * 10], r8
    mov [rax + 8 * 11], r9
    mov [rax + 8 * 12], r10
    mov [rax + 8 * 13], r11
    mov [rax + 8 * 14], r12
    mov [rax + 8 * 15], r13
    mov [rax + 8 * 16], r14
    mov [rax + 8 * 17], r15

    mov qword [rax + 8 * 19], ss
    mov qword [rax + 8 * 20], ds
    mov qword [rax + 8 * 21], fs
    mov qword [rax + 8 * 22], gs
    mov qword [rcx + 8 * 23], es

    mov rcx, rax
    pop rax
    mov [rcx + 8 * 2], rax

    pop rdi
    mov [rcx + 8 * 6], rdi

.store_rip:
    mov rax, [rsp + 8 * 0]
    mov [rcx + 8 * 1], rax

.store_cs:
    mov rax, [rsp + 8 * 1]
    mov [rcx + 8 * 18], rax

.store_rsp:
    mov rax, rsp
    add rax, 8 * 3
    mov [rcx + 8 * 9], rax

    jmp .call
.pop_rdi:
    pop rdi

.call:
    call clock_interrupt_handler
    iretq